package com.example.liang.basamovie.adapter;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.example.liang.basamovie.R;
import com.example.liang.basamovie.activity.PastSelectActivity;
import com.example.liang.basamovie.activity.WebActivity;
import com.example.liang.basamovie.activity.WebPlayerActivity;
import com.example.liang.basamovie.bean.Recommend;
import com.example.liang.basamovie.bean.Recommend2;
import com.example.liang.basamovie.bean.Recommend3;
import com.example.liang.basamovie.myview.MyBannerView;
import com.example.liang.basamovie.utils.ImageLoaderUtils;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class RecommendAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    Context context;
    List<Object> data;
    List<Object> data2;
    public RecommendAdapter(Context context, List<Object> data, List<Object> data2) {
        this.context = context;
        this.data = data;
        this.data2=data2;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==data.size()){
            return R.layout.item_recommend3;
        }else if(position==0){
            return R.layout.recommend_banner_layout;
        }
        else {
            Object o = data.get(position);
            if (o instanceof Recommend) {
                return R.layout.item_recommend1;
            } else {
                return R.layout.item_recommend2;
            }
        }
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(viewType, parent, false);
        if(viewType == R.layout.item_recommend1){
            return new OneViewHolder(view);
        }else if(viewType == R.layout.item_recommend2) {
            return new TwoViewHolder(view);
        }else if(viewType == R.layout.recommend_banner_layout) {
            return new FourViewHolder(view);
        }
        else {
            return new ThreeViewHolder(view);
        }
    }
    @Override
    public void onBindViewHolder( RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof OneViewHolder){
            final Recommend recommend = (Recommend) data.get(position);
            ((OneViewHolder) holder).title.setText(recommend.getTitle());
            ((OneViewHolder) holder).nickname.setText(recommend.getNickname());
            ((OneViewHolder) holder).type.setText(recommend.getType());
            long l = System.currentTimeMillis();
            int hour=(int)( (l/1000-recommend.getData()));
            if(hour>=0&&hour<=50000){
                ((OneViewHolder) holder).data.setText("今天");
            }else if(hour>50000&&hour<=128000){
                ((OneViewHolder) holder).data.setText("昨天");
            }else if(hour>128000&&hour<=210000){
                ((OneViewHolder) holder).data.setText("前天");
            }else if(hour>210000&&hour<=300000){
                ((OneViewHolder) holder).data.setText("3天前");
            }else if(hour>300000&&hour<=390000){
                ((OneViewHolder) holder).data.setText("4天前");
            }
            ImageLoaderUtils.setimage(context, recommend.getImage_url(), ((OneViewHolder) holder).picture);
            ((OneViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context,WebActivity.class);
                    intent.putExtra("url",recommend.getContentUrl());
                    intent.putExtra("title",recommend.getTitle());
                    context.startActivity(intent);
                }
            });
        }else  if(holder instanceof TwoViewHolder) {
            final Recommend2 recommend2 = ( Recommend2) data.get(position);
            ((TwoViewHolder) holder).title.setText(recommend2.getTitle());
            ((TwoViewHolder) holder).context.setText(recommend2.getDescription());
            ((TwoViewHolder) holder).nickname.setText(recommend2.getNickname());
            ((TwoViewHolder) holder).videotime.setText(recommend2.getVideotime());
            long l = System.currentTimeMillis();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date(l);
            String s = formatter.format(date);
            List<String> ss = new ArrayList<String>();
            for(String sss:s.replaceAll("[^0-9]", ",").split(",")){
                if (sss.length()>0)
                    ss.add(sss);
            }
            int  day = Integer.valueOf(ss.get(2));
            int year=Integer.valueOf(ss.get(0));
            int month=Integer.valueOf(ss.get(1));
            int hour=(int)( (l/1000-recommend2.getCreatetime()));
            if(hour>=0&&hour<100000){
                ((TwoViewHolder) holder).data.setText(s);
            }else if(hour>=100000&&hour<160000){
                ((TwoViewHolder) holder).data.setText(year+"-"+month+"-"+(day-1));
            }else if(hour>=160000&&hour<270000){
                ((TwoViewHolder) holder).data.setText(year+"-"+month+"-"+(day-2));
            }else if(hour>=270000&&hour<350000){
                ((TwoViewHolder) holder).data.setText(year+"-"+month+"-"+(day-3));
            }else if(hour>=350000&&hour<450000){
                ((TwoViewHolder) holder).data.setText(year+"-"+month+"-"+(day-4));
            }
            ImageLoaderUtils.setimage(context,recommend2.getImgurl(),((TwoViewHolder) holder).player);
            ((TwoViewHolder) holder).itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, WebPlayerActivity.class);
                    intent.putExtra("url",recommend2.getUrl());
                    intent.putExtra("videourl",recommend2.getVideoUrl());
                    context.startActivity(intent);
                }
            });
        }else  if(  holder instanceof FourViewHolder) {
           final Recommend3 recommend3=( Recommend3 ) data2.get(position);
            ((FourViewHolder) holder).mybannerview.setData(recommend3.getData());
            ((FourViewHolder) holder).mybannerview.setData1(recommend3.getData1());
            ((FourViewHolder) holder).mybannerview.setData2(recommend3.getData2());
            long l = System.currentTimeMillis();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");
            Date date = new Date(l);
            String s = formatter.format(date);
            List<String> ss = new ArrayList<String>();
            for(String sss:s.replaceAll("[^0-9]", ",").split(",")){
                if (sss.length()>0)
                    ss.add(sss);
            }
             int  hour = Integer.valueOf(ss.get(3));
            ((FourViewHolder) holder).changtime.setText(hour+"-----");
            if (hour>=0&&hour<=7){
                ((FourViewHolder) holder).changtime.setText("凌晨好");
            }else if(hour>7&&hour<=12){
                ((FourViewHolder) holder).changtime.setText("上午好");
            }else if(hour>12&&hour<=18){
                ((FourViewHolder) holder).changtime.setText("下午好");
            }else if(hour>18&&hour<=24){
                ((FourViewHolder) holder).changtime.setText("晚上好");
            }


        }
    }

    @Override
    public int getItemCount() {
        return data.size()+1;
    }
    class OneViewHolder extends RecyclerView.ViewHolder{
        TextView title,nickname,type,data;
        ImageView picture;
        public OneViewHolder(View itemView) {
            super(itemView);
             title = (TextView) itemView.findViewById(R.id.recommend1_title);
             nickname=(TextView) itemView.findViewById(R.id.recommend1_nickname);
             type = (TextView) itemView.findViewById(R.id.recommend1_type);
             picture=(ImageView)itemView.findViewById(R.id.recommend1_picture);
            data= (TextView) itemView.findViewById(R.id.recommend1_data);
        }
    }

    class TwoViewHolder extends RecyclerView.ViewHolder{
        TextView title,context,nickname,videotime,data;
        ImageView player;
        public TwoViewHolder(View itemView) {
            super(itemView);
            title=(TextView) itemView.findViewById(R.id.recommend2_title);
            context=(TextView) itemView.findViewById(R.id.recommend2_context);
            nickname=(TextView) itemView.findViewById(R.id.recommend2_nickname);
            videotime=(TextView) itemView.findViewById(R.id.recommend2_videotime);
            player=(ImageView)itemView.findViewById(R.id.recommend2_player);
            data=(TextView) itemView.findViewById(R.id.recommend2_data);
        }
    }
    class ThreeViewHolder extends RecyclerView.ViewHolder{
        LinearLayout linearLayout;
        public ThreeViewHolder(View itemView) {
            super(itemView);
            linearLayout=(LinearLayout) itemView.findViewById(R.id.recommend3_linearlayout);
            linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, PastSelectActivity.class);
                    context.startActivity(intent);
                }
            });
        }
    }
    class FourViewHolder extends RecyclerView.ViewHolder{
        TextView changtime;
        MyBannerView mybannerview;
        public FourViewHolder(View itemView) {
            super(itemView);
            changtime= (TextView) itemView.findViewById(R.id.text_changetime);
            mybannerview=(MyBannerView) itemView.findViewById(R.id.mybannerview);

        }
    }



}
